function timeZero(n) {
    return n < 10 ? "0" + n : n;
  }
  template.defaults.imports.dateFormate = function (cdate) {
    console.log(cdate);
    let date = new Date(cdate);
    let year = date.getFullYear();
    let mouth = date.getMonth() + 1;
    let day = date.getDate();
    let h = date.getHours();
    let min = date.getMinutes();
    let sec = date.getSeconds();
    return `${year}-${timeZero(mouth)}-${timeZero(day)} ${timeZero(h)}:${timeZero(
      min
    )}:${timeZero(sec)}`;
  };



$(function () {
    layui.form.verify({
        cate_name: [/^[\S]{1,10}$/, '请输入1到10位非空字符'],
        cate_content: [/^[\S]{1,1000}$/, '请输入1到1000位非空字符']
    })

    // 获取评论
    function getCate() {
        $.ajax({
            method: 'get',
            url: '/api/articles/1/comments',
            success: function (res) {
                console.log(res);
                const temp = template('mycomment', res)
                // console.log(temp);
                $('.observer').html(temp)
            }
        })
    }

    getCate()

    // function getCommentList() {
    //     $.ajax({
    //         type: 'get',
    //         url: '/api/articles/1/comments',
    //         success: function (res) {
    //             if (res.status === 0) {
    //                 var arr = ['<h4><i class="sprites"></i>评论区</h4>']
    //                 res.data.forEach(function (item) {
    //                     arr.push(`
    //                     <div class="kr_comment_card">
    //                     <div class="img-wrap"><img src="./uploads/avatar_3.jpg" alt=""></div>
    //                     <div class="info"><p>${item.uname} · <span>${item.cdate}</span></p>
    //                         <p>${item.content}</p>
    //                     </div>
    //                         <a href=" " class="like">${item.count}</a>
    //                 </div>
    //                     `)
    //                 })

    //                 $('#commentList').html(arr.join(''))
    //             }
    //         }

    //     })
    // }

    // getCommentList()


    // 发表评论
    $('body').on('submit', '#comment-form', function (e) {
        e.preventDefault()
        console.log($(this).serialize());
        $.ajax({
            url: '/api/articles/1/comments',
            method: 'POST',
            data: $(this).serialize(),
            success: function (res) {
                // console.log(res);
                if (res.status === 0) {
                    layui.layer.msg(res.message)
                    $('#comment-form').get(0).reset()
                    getCate()
                    // getCommentList()

                }
            }
        })
    })
})


